var pathName=window.location.pathname;
var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
 
function to_select_option(select_option) {
	var div = $("select_array");
	for(var i = 0; i < select_option.length; i++) {
		var select_div = document.createElement("div");
		select_div.setAttribute("class", "am-u-sm-12 am-u-md-3");
		var select_child_div = document.createElement("div");
		select_child_div.setAttribute("class", "am-form-group");
		var select = document.createElement("select");
		select.setAttribute("data-am-selected", "{btnSize: 'sm'}");
		for(var j = 0; j < select_option[0].length; j++) {
			
		}
		select_child_div.appendChild(select);
		select_div.appendChild(select_child_div);
		div.appendChild(select);
	}
}    
    
function to_page(pn, serviceName, tableTh, pto){
	var json = "page="+pn;
	
	var select = $("#pigTypeSelect");
	var option = select.children('option:selected');
	var opval = option.val();
	if(opval == "0" || opval != "" && !$.trim(opval).length == 0){
	    json += "&pigType="+opval;
	}
	
	
	var select1 = $("#growthSelect");
	var option1 = select1.children('option:selected');
	var opval1 = option1.val();
	if(opval == "0" || opval1 != "" && !$.trim(opval1).length == 0){
		json += "&growth="+opval1;
	}
	
	
    $.ajax({
        url:projectName + serviceToPath[serviceName],
        data:json,
        type:"get",
        success:function(result){
            build_sowfens_table(result, tableTh, pto);
            //解析并显示分页信息
            build_page_info(result);
            build_page_nav(result, serviceName, tableTh, pto);
        }
    });
}

function build_sowfens_table(result, tableTh, pto){

    $("#pagetable tbody").empty();
    $("#pagetable thead").empty();
    
    var tabledatas=result.extend.data.tableData;
    
    var tr = $("<tr></tr>");
    var input = document.createElement("input");
    input.setAttribute("class", "tpl-table-fz-check");
    input.setAttribute("type", "checkbox");
    
    tr.append($("<th></th>").append(input));
    Object.keys(tableTh).forEach(function(key){
        tr.append($("<th></th>").append(tableTh[key]));
    });
    
    $.each(tabledatas,function(index,tabledata){
        var tr1 = $("<tr></tr>");
        var input1 = document.createElement("input");
        input1.setAttribute("class", "tpl-table-fz-check");
        input1.setAttribute("type", "checkbox");
        tr1.append($("<td></td>").append(input1));
        
        Object.keys(tableTh).forEach(function(key){
        	var value = tabledata[key];
        	if(key == "sex") {
        		if(value == "1") {
        			value = "母猪";
        		} else if(value == "0") {
        			value = "公猪";
        		}
        	}
        	
        	tr1.append($("<td></td>").append(value));
        });
        var td = $("<td></td>");
        var div = $("<div></div>");
        var div1 = $("<div></div>");
        div.addClass("am-btn-toolbar");
        div1.addClass("am-btn-group am-btn-group-xs");
        if(pto != null) {
        Object.keys(pto).forEach(function(key){
        	var Btn = $("<button></button>");
        	Btn.addClass(pto[key].str);
        	var pto1 = pto[key].btnType;
        	Object.keys(pto1).forEach(function(key){
        		Btn.attr(key, pto1[key]);
            });
        	Btn.append(key);
        	div1.append(Btn);
        });
        }
        div.append(div1);
        td.append(div);
        tr1.append(td);
        tr1.appendTo("#pagetable tbody");});
    
    if(pto != null) {
        tr.append($("<th></th>").append("操作"));
    }
    
    
    tr.appendTo("#pagetable thead");
  }
    
  
    function build_page_info(result){
    	totalRecord = result.extend.data.totle;
        currentPage = result.extend.data.page;
    	totalPage = result.extend.data.totalPage;
        $("#page_info_area").empty();
    $("#page_info_area").append("当前"+currentPage+"页,总"+
    		totalPage+"页,总"+
    		totalRecord+"条记录");
    
}

function build_page_nav(result, serviceName, tableTh, pto){
    //page_nav_area
    $("#page_nav_area").empty();
    var ul = $("<ul></ul>").addClass("am-pagination tpl-pagination");
    //构建元素
    var firstPageLi = $("<li></li>").append($("<a></a>").append("首页").attr("href","#"));
    var prePageLi = $("<li></li>").append($("<a></a>").append("&laquo;"));
    if(currentPage == 1){
        firstPageLi.addClass("disabled");
        prePageLi.addClass("disabled");
    }else{
        //为元素添加点击翻页的事件
        firstPageLi.click(function(){
            to_page(1, serviceName, tableTh, pto);
        });
        prePageLi.click(function(){
            to_page(currentPage -1, serviceName, tableTh, pto);
        });
    }
    
    var nextPageLi = $("<li></li>").append($("<a></a>").append("&raquo;"));
    var lastPageLi = $("<li></li>").append($("<a></a>").append("末页").attr("href","#"));
    if(currentPage == totalPage){
        nextPageLi.addClass("disabled");
        lastPageLi.addClass("disabled");
    }else{
        nextPageLi.click(function(){
            to_page(currentPage + 1, serviceName, tableTh, pto);
        });
        lastPageLi.click(function(){
            to_page(totalPage, serviceName, tableTh, pto);
        });
    }
    
    var nums = [];
    var prePageNum = currentPage - 2;
    
    if(totalPage <= 5) {
    	for(var i = 0; i < totalPage; i++) {
    		nums[i] = i + 1;
    	}
    } else if(currentPage - totalPage < 5) {
    	for(var i = 0; i < 5; i++) {
            nums[i] = totalPage - 4 + i;
        }
    } else {
        for(var i = 0; i < 5; i++) {
        	var pagel = i + prePageNum;
        	nums[i] = pagel;
        	
        }
    }
    
    //添加首页和前一页 的提示
    ul.append(firstPageLi).append(prePageLi);
    //1,2，3遍历给ul中添加页码提示
    $.each(nums, function(index,item){
        
        var numLi = $("<li></li>").append($("<a></a>").append(item));
        if(currentPage == item){
            numLi.addClass("active");
        }
        numLi.click(function(){
            to_page(item, serviceName, tableTh, pto);
        });
        ul.append(numLi);
    });
    //添加下一页和末页 的提示
    ul.append(nextPageLi).append(lastPageLi);
    //把ul加入到nav
    var navEle = $("<nav></nav>").append(ul);
    navEle.appendTo("#page_nav_area");
}